package test.util;

import org.junit.After;
import org.junit.BeforeClass;

import com.guxingke.typecho4j.jfinal.model.Comment;
import com.guxingke.typecho4j.jfinal.model.Content;
import com.guxingke.typecho4j.jfinal.model.Meta;
import com.guxingke.typecho4j.jfinal.model.Option;
import com.guxingke.typecho4j.jfinal.model.User;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;

public class JFinalModelTestCaseUtil {
	protected static C3p0Plugin c3p0Plugin;
	protected static ActiveRecordPlugin arp;

	/**
	 * 数据连接地址
	 */
	private static final String URL = "jdbc:mysql://localhost/typecho?useUnicode=true&characterEncoding=UTF-8";

	/**
	 * 数据库账号
	 */
	private static final String USERNAME = "root";

	/**
	 * 数据库密码
	 */
	private static final String PASSWORD = "123456";

	/**
	 * 数据库驱动
	 */
	private static final String DRIVER = "com.mysql.jdbc.Driver";

	/**
	 * 数据库类型（如mysql，oracle）
	 */
	// private static final String DATABASE_TYPE = "mysql";

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {

		c3p0Plugin = new C3p0Plugin(URL, USERNAME, PASSWORD, DRIVER);
		c3p0Plugin.getDataSource();
		c3p0Plugin.start();

		arp = new ActiveRecordPlugin(c3p0Plugin);
		arp.setDialect(new MysqlDialect()).setDevMode(true)
				.setShowSql(true); // 是否打印sql语句

		// 映射数据库的表和继承与model的实体
		// 只有做完该映射后，才能进行junit测试
		arp.addMapping("typecho_contents", "cid", Content.class);
		arp.addMapping("typecho_metas", "mid", Meta.class);
		arp.addMapping("typecho_comments", "coid", Comment.class);
		arp.addMapping("typecho_options", "name", Option.class);
		arp.addMapping("typecho_users", "uid", User.class);

		arp.start();
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		arp.stop();
		c3p0Plugin.stop();
	}
}
